package test;

import compound.*;
import compound.entity.duck.DuckCall;
import compound.entity.duck.MallardDuck;
import compound.entity.duck.RedHeadDuck;
import compound.entity.duck.RubberDuck;
import compound.entity.goose.Goose;
import compound.observer.Quackologist;

//compound 复合模式的测试
public class DuckSimulator {
    public static void main(String[] args) {
        DuckSimulator simulator = new DuckSimulator();
        AbstractDuckFactory duckFactory = new CountingDuckFactory();
        simulator.simulate();
        System.out.println("==========================================");
        simulator.simulate(duckFactory);
        System.out.println("==========================================");
        simulator.simulate1(duckFactory);
        System.out.println("==========================================");
        simulator.simulate2(duckFactory);
        System.out.println("==========================================");
    }

    //普通模拟器
    private void simulate() {
        QuackAble mallardDuck = new QuackCounter(new MallardDuck());
        QuackAble redHeadDuck = new QuackCounter(new RedHeadDuck());
        QuackAble duckCall = new QuackCounter(new DuckCall());
        QuackAble rubberDuck = new QuackCounter(new RubberDuck());

        QuackAble gooseDuck = new GooseAdapter(new Goose());

        System.out.println("鸭子模拟器:");
        simulate(mallardDuck);
        simulate(redHeadDuck);
        simulate(duckCall);
        simulate(rubberDuck);
        simulate(gooseDuck);

        System.out.println("鸭子叫了" + QuackCounter.getQuacks() + "次");
    }

    //与抽象工厂复合的鸭子模拟器
    private void simulate(AbstractDuckFactory duckFactory) {
        QuackAble mallardDuck = duckFactory.createMallardDuck();
        QuackAble redHeadDuck = duckFactory.createRedHeadDuck();
        QuackAble duckCall = duckFactory.createDuckCall();
        QuackAble rubberDuck = duckFactory.createRubberDuck();

        QuackAble gooseDuck = new GooseAdapter(new Goose());

        System.out.println("鸭子模拟器:");
        simulate(mallardDuck);
        simulate(redHeadDuck);
        simulate(duckCall);
        simulate(rubberDuck);
        simulate(gooseDuck);

        System.out.println("鸭子叫了" + QuackCounter.getQuacks() + "次");
    }

    //再加上组合模式的鸭子模拟器
    private void simulate1(AbstractDuckFactory duckFactory) {
        QuackAble redHeadDuck = duckFactory.createRedHeadDuck();
        QuackAble duckCall = duckFactory.createDuckCall();
        QuackAble rubberDuck = duckFactory.createRubberDuck();

        QuackAble gooseDuck = new GooseAdapter(new Goose());

        System.out.println("鸭子模拟器:");
        Flock flockOfDucks = new Flock();
        flockOfDucks.add(redHeadDuck);
        flockOfDucks.add(duckCall);
        flockOfDucks.add(rubberDuck);
        flockOfDucks.add(gooseDuck);

        Flock flockOfMallards = new Flock();
        QuackAble mallard1 = duckFactory.createMallardDuck();
        QuackAble mallard2 = duckFactory.createMallardDuck();
        QuackAble mallard3 = duckFactory.createMallardDuck();
        QuackAble mallard4 = duckFactory.createMallardDuck();
        flockOfMallards.add(mallard1);
        flockOfMallards.add(mallard2);
        flockOfMallards.add(mallard3);
        flockOfMallards.add(mallard4);

        flockOfDucks.add(flockOfMallards);
        System.out.println("测试一整群鸭子");
        simulate(flockOfDucks);
        System.out.println("测试绿头鸭");
        simulate(flockOfMallards);

        System.out.println("鸭子叫了" + QuackCounter.getQuacks() + "次");
    }

    //观察者模式的鸭子模拟器
    private void simulate2(AbstractDuckFactory duckFactory) {
        QuackAble redHeadDuck = duckFactory.createRedHeadDuck();
        QuackAble duckCall = duckFactory.createDuckCall();
        QuackAble rubberDuck = duckFactory.createRubberDuck();

        QuackAble gooseDuck = new GooseAdapter(new Goose());

        System.out.println("鸭子模拟器:");
        Flock flockOfDucks = new Flock();
        flockOfDucks.add(redHeadDuck);
        flockOfDucks.add(duckCall);
        flockOfDucks.add(rubberDuck);
        flockOfDucks.add(gooseDuck);

        Flock flockOfMallards = new Flock();
        QuackAble mallard1 = duckFactory.createMallardDuck();
        QuackAble mallard2 = duckFactory.createMallardDuck();
        QuackAble mallard3 = duckFactory.createMallardDuck();
        QuackAble mallard4 = duckFactory.createMallardDuck();
        flockOfMallards.add(mallard1);
        flockOfMallards.add(mallard2);
        flockOfMallards.add(mallard3);
        flockOfMallards.add(mallard4);

        flockOfDucks.add(flockOfMallards);
        System.out.println("测试一整群鸭子");
        Quackologist quackologist = new Quackologist();
        flockOfDucks.registerObserver(quackologist);
        simulate(flockOfDucks);

        flockOfDucks.notifyObservers();

        System.out.println("鸭子叫了" + QuackCounter.getQuacks() + "次");
    }

    private void simulate(QuackAble duck) {
        duck.quack();
    }
}
